import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    user: {},
    // 在 state 中存储初始状态
    initialState: {
      user: {},
    },
  },
  getters: {},
  mutations: {
    setUser(state, user) {
      state.user = user;
    },
    resetState: (state) => {
      // 使用 state 中的 initialState
      Object.assign(state, state.initialState);
    },
  },
  actions: {
    updateUser({ commit }, user) {
      commit('setUser', user);
    },
    resetState({ commit }) {
      commit('resetState');
    },
  },
  modules: {},
  plugins: [createPersistedState()],
});
